package com.dabai.iot.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dabai.common.core.domain.R;
import com.dabai.common.mybatis.core.page.PageQuery;
import com.dabai.common.mybatis.core.page.TableDataInfo;
import com.dabai.common.web.core.BaseController;
import com.dabai.iot.core.convert.DeviceConvert;
import com.dabai.iot.core.dto.DeviceInput;
import com.dabai.iot.core.dto.DeviceQuery;
import com.dabai.iot.core.dto.DeviceUpdate;
import com.dabai.iot.core.entity.DeviceEntity;
import com.dabai.iot.core.entity.ProductTopicEntity;
import com.dabai.iot.core.model.MqttLinkInfo;
import com.dabai.iot.core.service.IDeviceService;
import com.dabai.iot.core.vo.DeviceSelectVO;
import com.dabai.iot.core.vo.DeviceVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备管理
 *
 * @author kev1n
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController {

    private final IDeviceService deviceService;

    /**
     * 新增设备
     */
    @PostMapping("/add")
    @Operation(summary = "新增设备")
    public R<Void> add(@Valid @RequestBody DeviceInput deviceInput) {
        DeviceEntity device = DeviceConvert.INSTANCE.convert(deviceInput);
        boolean result = deviceService.addDevice(device);
        return toAjax(result);
    }

    /**
     * 修改设备
     */
    @PostMapping("/update")
    @Operation(summary = "修改设备")
    public R<Void> update(@Valid @RequestBody DeviceUpdate deviceUpdate) {
        DeviceEntity device = deviceService.getById(deviceUpdate.getId());
        if (device == null) {
            return R.fail("设备不存在");
        }

        device.setDeviceName(deviceUpdate.getDeviceName());
        device.setDeviceDesc(deviceUpdate.getDeviceDesc());
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除设备
     */
    @PostMapping("/remove")
    @Operation(summary = "删除设备", description = "传入主键id")
    public R<Void> remove(@Parameter(description = "主键id", required = true) @RequestParam Long id) {
        boolean result = deviceService.removeDevice(id);
        return toAjax(result);
    }

    /**
     * 设备详情
     */
    @GetMapping("/detail")
    @Operation(summary = "获取设备详情", description = "传入主键id")
    public R<DeviceVO> detail(@RequestParam Long id) {
        return R.ok(deviceService.detailById(id));
    }

    /**
     * 设备列表分页
     */
    @GetMapping("/page")
    @Operation(summary = "设备列表分页", description = "传入对应查询参数")
    public TableDataInfo<DeviceVO> page(PageQuery pageQuery, DeviceQuery deviceQuery) {
        IPage<DeviceVO> pages = deviceService.getDevicePage(pageQuery.build(), deviceQuery);
        return TableDataInfo.build(pages);
    }

    /**
     * 设备下拉数据源
     */
    @GetMapping("/select")
    @Operation(summary = "设备下拉数据源", description = "用于设备选择")
    public R<List<DeviceSelectVO>> select(DeviceQuery deviceQuery) {
        return R.ok(deviceService.select(deviceQuery));
    }

    /**
     * 按设备分组返回设备列表分页
     */
    @GetMapping("/pageByGroup")
    @Operation(summary = "按设备分组返回设备列表分页", description = "传入对应查询参数")
    public TableDataInfo<DeviceVO> pageByGroup(PageQuery pageQuery, Long groupId, DeviceQuery deviceQuery) {
        IPage<DeviceVO> pages = deviceService.getDevicePageByGroup(pageQuery.build(), groupId, deviceQuery);
        return TableDataInfo.build(pages);
    }

    /**
     * 设备主题分页
     */
    @GetMapping("/topic/page")
    @Parameters({
        @Parameter(name = "deviceId", description = "设备Id", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "topicSourceType", description = "Topic 所属类别", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "topicDirection", description = "Topic 通信方向", in = ParameterIn.QUERY, schema = @Schema(type = "string"))
    })
    @Operation(summary = "设备(MQTT)Topic 分页", description = "传入对应查询参数")
    public TableDataInfo<ProductTopicEntity> deviceTopicPage(PageQuery pageQuery, @RequestParam Long deviceId, @RequestParam String topicSourceType, String topicDirection) {
        return deviceService.getTopicPage(pageQuery.build(), deviceId, topicSourceType, topicDirection);
    }

    /**
     * 查看设备 MQTT 连接参数
     */
    @GetMapping("/linkInfos")
    @Operation(summary = "查看 MQTT 设备连接参数")
    public R<MqttLinkInfo> getConnectParams(@RequestParam Long deviceId) {
        return R.ok(deviceService.getMqttLinkInfo(deviceId));
    }

}
